package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.enumclass.ProductStatus;
import com.rockcent.mall.lib.domain.enumclass.ProductType;
import com.rockcent.mall.lib.dto.MallProductAuditDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

/**
 * Created by zhangjh on 2016/10/10.
 */

@Repository
public class MallProductAuditRepositoryImpl implements MallProductAuditRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MallProductAuditRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<MallProductAuditDto> listProductIsAudit(String productName, String merchantName, String auditStatus, String type, String startAuditDate, String endAuditDate, Whether isPass,Long mallId, PageResult pageResult) {
        SQLPageDoing doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall_product_audit pa");
            sql.append(" LEFT JOIN product p ON p.id=pa.product_id");
            sql.append(" LEFT JOIN `merchant` m ON m.`id`=pa.merchant_id");
            sql.append(" WHERE pa.is_delete='NO' And m.mall_id=:mallId");
            params.put("mallId", mallId);
            if (isPass != null) {

                sql.append(" AND pa.is_pass=:is_pass");
                params.put("is_pass", isPass.toString());
            }

            if (StringUtils.isNotBlank(productName)) {
                sql.append(" AND (p.name like:productName OR p.barcode like:barcode)");
                params.put("productName", "%"+productName +"%");
                params.put("barcode", "%"+productName +"%");
            }
            if (StringUtils.isNotBlank(merchantName)) {
                sql.append(" AND m.name like :merchantName");
                params.put("merchantName", "%"+merchantName +"%");

            }
            if (StringUtils.isNotBlank(type) && !ProductType.ALL.toString().equals(type)) {
                sql.append(" AND p.type=:type");
                params.put("type", type);
            }
            if(isPass==Whether.YES){
                //已审核
                if (StringUtils.isNotBlank(auditStatus)) {
                    sql.append(" AND pa.status=:auditStatus");
                    params.put("auditStatus", auditStatus);
                }
            }else {
                //未审核
                sql.append(" AND p.status in (:status) ");
                params.put("status", Arrays.asList(ProductStatus.AUDIT_FAIL.toString(), ProductStatus.WAIT_AUDIT.toString()));
            }

//            if (StringUtils.isNotBlank(auditStatus)) {
//                if(ProductStatus.valueOf(auditStatus)==ProductStatus.WAIT_AUDIT){
//                    sql.append(" AND p.status in (:status) ");
//                    params.put("status", Arrays.asList(ProductStatus.AUDIT_FAIL.toString(), ProductStatus.WAIT_AUDIT.toString()));
//                }else {
//                    sql.append(" AND pa.status=:auditStatus");
//                    params.put("auditStatus", auditStatus);
//                }
//            }

            if (StringUtils.isNotBlank(startAuditDate) && StringUtils.isNotBlank(endAuditDate)) {
                sql.append(" AND pa.audit_date > :startAuditDate AND pa.audit_date < :endAuditDate");
                params.put("startAuditDate", RDateUtils.beforeDateStr(startAuditDate, 0));
                params.put("endAuditDate", RDateUtils.afterDateStr(endAuditDate, 1));

            } else if (StringUtils.isNotBlank(startAuditDate)) {
                sql.append(" AND pa.audit_date  > :startAuditDate");
                params.put("startAuditDate", RDateUtils.beforeDateStr(startAuditDate, 0));
            } else if (StringUtils.isNotBlank(endAuditDate)) {
                sql.append(" AND pa.audit_date < :endAuditDate");
                params.put("endAuditDate", RDateUtils.afterDateStr(endAuditDate, 1));
            }
            if(isPass==Whether.NO){
                sql.append(" ORDER BY pa.last_updated desc");

                fields.append("pa.product_id as productId,pa.id,pa.merchant_name,p.barcode,p.name as productName,m.name as merchantName,pa.type, pa.date_created," +
                              " pa.last_updated,pa.is_pass");
            }else {
                sql.append(" ORDER BY pa.audit_date desc");
                fields.append("pa.id,pa.merchant_name,p.barcode,p.name as productName,m.name as merchantName,pa.type,pa.audit_date,pa.status,pa.is_pass");
            }
        };

        return doing.go(pageResult, MallProductAuditDto.class, objectMapper, jdbcTemplate, log);
    }
}
